#!/bin/bash
set -e
set -o pipefail

# Detect where build_rive.sh is located on disk.
# https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
    SOURCE=$(readlink "$SOURCE")
    # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
    [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done
SCRIPT_DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )

if [[ "$1" = "rebuild" ]]; then
    # Load args from an existing build.
    RIVE_OUT=$2
    shift
    shift

    if [ ! -d $RIVE_OUT ]; then
        echo "OUT directory '$RIVE_OUT' not found."
        exit -1
    fi

    ARGS_FILE=$RIVE_OUT/.rive_premake_args
    if [ ! -f $ARGS_FILE ]; then
        echo "Premake args file '$ARGS_FILE' not found."
        exit -1
    fi

    RIVE_PREMAKE_ARGS="$(< $ARGS_FILE)"
    RIVE_BUILD_SYSTEM="$(awk '{print $1}' $ARGS_FILE)" # RIVE_BUILD_SYSTEM is the first word in the args.
    if grep -e "--arch=" $ARGS_FILE > /dev/null; then
        RIVE_ARCH="$(sed -r 's/^.*--arch=([^ ]*).*$/\1/' $ARGS_FILE)"
    fi
else
    # New build. Parse arguments into premake options.

    # Only use default arguments if RIVE_PREMAKE_ARGS is unset (not just empty).
    if [ -z "${RIVE_PREMAKE_ARGS+null_detector_string}" ]; then
        RIVE_PREMAKE_ARGS="--with_rive_text --with_rive_layout"
    fi

    while [[ $# -gt 0 ]]; do
        case "$1" in
            "debug") RIVE_CONFIG="${RIVE_CONFIG:-debug}" ;;
            "release") RIVE_CONFIG="${RIVE_CONFIG:-release}" ;;
            "arm64") RIVE_ARCH="${RIVE_ARCH:-arm64}" ;;
            "arm") RIVE_ARCH="${RIVE_ARCH:-arm}" ;;
            "x64") RIVE_ARCH="${RIVE_ARCH:-x64}" ;;
            "x86") RIVE_ARCH="${RIVE_ARCH:-x86}" ;;
            "ninja") RIVE_BUILD_SYSTEM="${RIVE_BUILD_SYSTEM:-ninja}" ;;
            "clean") RIVE_CLEAN="${RIVE_CLEAN:-true}" ;;
            "--")
                shift
                break
                ;;
            *) RIVE_PREMAKE_ARGS="$RIVE_PREMAKE_ARGS $1" ;;
        esac
        shift
    done

    RIVE_CONFIG="${RIVE_CONFIG:-debug}"

    if [ -z "$RIVE_OUT" ]; then
        RIVE_OUT="$RIVE_CONFIG"
        if [ ! -z "$RIVE_ARCH" ]; then
            RIVE_OUT="${RIVE_ARCH}_$RIVE_OUT"
        fi
        if [[ $RIVE_VARIANT = "emulator" ]]; then
            RIVE_OUT="${RIVE_OS}sim_$RIVE_OUT"
        elif [ ! -z "$RIVE_OS" ]; then
            RIVE_OUT="${RIVE_OS}_$RIVE_OUT"
        fi
        RIVE_OUT="out/$RIVE_OUT"
    fi

    RIVE_BUILD_SYSTEM="${RIVE_BUILD_SYSTEM:-ninja}"
    RIVE_PREMAKE_ARGS="$RIVE_BUILD_SYSTEM --config=$RIVE_CONFIG --out=$RIVE_OUT $RIVE_PREMAKE_ARGS"
    if [[ $RIVE_OS = "ohos" ]]; then
        RIVE_PREMAKE_ARGS="$RIVE_PREMAKE_ARGS --for_ohos"
    elif [ ! -z "$RIVE_OS" ]; then
        RIVE_PREMAKE_ARGS="$RIVE_PREMAKE_ARGS --os=$RIVE_OS"
    fi
    if [ ! -z "$RIVE_VARIANT" ]; then RIVE_PREMAKE_ARGS="$RIVE_PREMAKE_ARGS --variant=$RIVE_VARIANT"; fi
    if [ ! -z "$RIVE_ARCH" ]; then RIVE_PREMAKE_ARGS="$RIVE_PREMAKE_ARGS --arch=$RIVE_ARCH"; fi

    if [[ "$RIVE_CLEAN" = true ]]; then
        rm -fr "./$RIVE_OUT"
    fi
fi

mkdir -p "$SCRIPT_DIR/dependencies"
pushd "$SCRIPT_DIR/dependencies" > /dev/null

# Add Rive's build scripts to the premake path.
export PREMAKE_PATH="$SCRIPT_DIR"

# Setup premake-ninja.
if [[ $RIVE_BUILD_SYSTEM = "ninja" ]]; then
    if [ ! -d premake-ninja ]; then
        git clone --branch rive_modifications https://gitee.com/yp9522/premake-ninja.git
    fi
    export PREMAKE_PATH="$SCRIPT_DIR/dependencies/premake-ninja:$PREMAKE_PATH"
fi
popd > /dev/null # leave "$SCRIPT_DIR/dependencies"

if [[ -d "$RIVE_OUT" ]]; then
    if [[ "$RIVE_PREMAKE_ARGS" != "$(< $RIVE_OUT/.rive_premake_args)" ]]; then
        echo "error: premake5 arguments for current build do not match previous arguments"
        echo "  previous command: premake5 $(< $RIVE_OUT/.rive_premake_args)"
        echo "   current command: premake5 $RIVE_PREMAKE_ARGS"
        echo "If you wish to overwrite the existing build, please use 'clean'"
        exit -1
    fi
else
    mkdir -p "$RIVE_OUT"
    echo "$RIVE_PREMAKE_ARGS" > "$RIVE_OUT/.rive_premake_args"
fi

echo premake5 $RIVE_PREMAKE_ARGS
premake5 $RIVE_PREMAKE_ARGS

echo ninja -C $RIVE_OUT $@
ninja -C $RIVE_OUT $@
